package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.DestinationSearch;
import cn.wolfcode.luowowo.article.domain.StrategyCatalog;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.*;
import cn.wolfcode.luowowo.common.query.DestinationQueryObject;
import cn.wolfcode.luowowo.common.query.DestinationSearchQueryObject;
import cn.wolfcode.luowowo.common.query.TravelQueryObject;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("destination")
public class DestinationController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationThemeService destinationThemeService;
    @Reference
    private IDestinationSearchService searchService;

    @RequestMapping("")
    public String index(Model model){
        model.addAttribute("hotRegions",regionService.listHotRegion());
        model.addAttribute("yearlyDestTheme",destinationThemeService.selectByTypeLimit12(new Integer(1)));
        model.addAttribute("seasonDestTheme",destinationThemeService.selectByType(new Integer(2)));
        model.addAttribute("tripModeDestTheme",destinationThemeService.selectByType(new Integer(3)));
        model.addAttribute("holidayDestTheme",destinationThemeService.selectByType(new Integer(4)));
        return "destination/index";
    }

    @RequestMapping("listHotDestByRegionId")
    public String listHotDestByRegionId(@ModelAttribute("regionId") Long regionId, Model model){
        //获取该区域id下的所有目的地
        List<Destination> destinations = destinationService.listHotDestByRegionId(regionId);
        //分左半部分和右半部分
        List<Destination> leftDest = destinations.subList(0, destinations.size() / 2);
        List<Destination> rightDest = destinations.subList(destinations.size() / 2, destinations.size());
        model.addAttribute("leftDests",leftDest);
        model.addAttribute("rightDests",rightDest);
        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public String guide(Long id, Model model){
        //拿到吐司数据,显示到页面上
        List<Destination> toasts = destinationService.getToasts(id);
        //拿到当前目的地的分类对象放到集合里面, 返回给前段显示在"概况"里面
        List<StrategyCatalog> catalogs = strategyCatalogService.listByDestId(id);

        //strategyDetails 挑选浏览数最多的三篇攻略文章
        model.addAttribute("strategyDetails",strategyDetailService.listViewnumTop3(id));
        //catalogs
        model.addAttribute("catalogs",catalogs);
        // 吐司toasts
        model.addAttribute("toasts",toasts);
        // 目的地dest
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));
        return "destination/guide";
    }

    @RequestMapping("/surveyPage")
    public String surveyPage(@ModelAttribute("qo") DestinationQueryObject qo, Model model){
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        // 吐司toasts
        model.addAttribute("toasts",toasts);
        // 目的地dest
        model.addAttribute("dest", toasts.remove(toasts.size() - 1));
        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(@ModelAttribute("qo") DestinationQueryObject qo, Model model){
        //catalogs , 根据destId查询它所有的分类
        List<StrategyCatalog> catalogs = strategyCatalogService.listByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);
        //catalog
        StrategyCatalog catalog = null;
        for (StrategyCatalog c : catalogs) {
            if (c.getId().equals(qo.getCatalogId())) {
                catalog = c;
                break;
            }
        }
        model.addAttribute("catalog",catalog);
        //detail, 当前目的地下所有攻略的第一篇
        Long detailId = catalog.getDetails().get(0).getId();
        StrategyDetail detail = strategyDetailService.getById(detailId);
        model.addAttribute("detail",detail);
        return "destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public String travels(@ModelAttribute("qo")TravelQueryObject qo,Model model){
        //pageInfo
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "destination/travelTpl";
    }

    @RequestMapping("/destFilter")
    public String destFilter(@ModelAttribute("qo") DestinationSearchQueryObject qo, Model model){
        model.addAttribute("yearlyDestTheme",destinationThemeService.selectByType(new Integer(1)));
        model.addAttribute("seasonDestTheme",destinationThemeService.selectByType(new Integer(2)));
        model.addAttribute("tripModeDestTheme",destinationThemeService.selectByType(new Integer(3)));
        model.addAttribute("holidayDestTheme",destinationThemeService.selectByType(new Integer(4)));
        model.addAttribute("dayDestTheme",destinationThemeService.selectByType(new Integer(5)));
        model.addAttribute("monthDestTheme",destinationThemeService.selectByType(new Integer(6)));
        return "destination/destFilter";
    }
    @RequestMapping("/destFilterTpl")
    public String destFilterTpl(@ModelAttribute("qo") DestinationSearchQueryObject qo, Model model){
        model.addAttribute("pageInfo", searchService.query(qo));
        return "destination/destFilterTpl";
    }

}
